package codingforgreat.lchot150;

public class Lc238 {
    public int[] productExceptSelf(int[] nums) {
        int N = nums.length;
        int[] ans = new int[nums.length];
        ans[N - 1] = nums[N - 1];
        for(int i = N - 2;i >=0;i--){
            ans[i] = ans[i + 1] * nums[i];
        }
        int head = 1;
        for(int i = 0;i < N;i++){
            if(i < N - 1){
                ans[i] = ans[i + 1] * head;
                head *= nums[i];
            }else{
                ans[i] = head;
            }
        }
        return ans;
    }
}
